package br.com.ffm.praticas.facade;

import java.sql.SQLException;
import java.util.List;

import br.com.ffm.praticas.bean.LegalCostumer;
import br.com.ffm.praticas.dao.LegalCostumerDAO;

public class LegalCostumerFACADE {
	
	private static LegalCostumerFACADE legalCostumerFACADE;
	private static LegalCostumerDAO legalCostumerDAO;
	
	private LegalCostumerFACADE() {
		legalCostumerDAO = new LegalCostumerDAO();
	}
	
	/**
	 * Method that returns a unique instance of LegalCostumerFACADE
	 * @return Returns a Instance of LegalCostumerFACADE
	 */
	public static LegalCostumerFACADE getInstance() {
		if (legalCostumerFACADE == null)
			legalCostumerFACADE = new LegalCostumerFACADE();
		return legalCostumerFACADE;
	}
	
	/**
	 * Method that tests if the Legal Costumer already exist
	 * @return false if the Legal Costumer already exist
	 * @throws SQLException 
	 */
	private boolean alreadyExistLegalCostumer(LegalCostumer legalCostumer) throws SQLException{
		if (legalCostumer != null) {
			LegalCostumer lc = legalCostumerDAO.getLegalCostumer(legalCostumer.getId());
			if (lc != null)
				return true;
			else
				return false;
		}
		return false;
	}
	
	/**
	 * Method that adds a Legal Costumer
	 * @param legalCostumer Legal Costumer
	 * @return false if the Legal Costumer already exist
	 * @throws SQLException 
	 */
	public boolean insert(LegalCostumer legalCostumer) throws SQLException {
		if (!alreadyExistLegalCostumer(legalCostumer)) {
			legalCostumerDAO.insert(legalCostumer);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that removes a Legal Costumer
	 * @param legalCostumer Legal Costumer
	 * @return false if the Legal Costumer don't exist
	 */
	public boolean delete(LegalCostumer legalCostumer) {
		return legalCostumerDAO.delete(legalCostumer);
	}
	
	/**
	 * Method that looks for a Legal Costumer
	 * @param id Legal Costumer id
	 * @return null if the Legal Costumer don't exist
	 * @throws SQLException 
	 */
	public LegalCostumer getLegalCostumer(int id) throws SQLException {
		return legalCostumerDAO.getLegalCostumer(id);
	}
	
	/**
	 * Method that looks for a Legal Costumer
	 * @param cnpj Legal Costumer CNPJ
	 * @return null if the Legal Costumer don't exist
	 * @throws SQLException 
	 */
	public LegalCostumer getLegalCostumer(String cnpj) throws SQLException {
		return legalCostumerDAO.getLegalCostumer(cnpj);
	}
	
	/**
	 * Method that returns all LegalCostumers
	 * @return The List of all LegalCostumers
	 * @throws SQLException 
	 */
	public List<LegalCostumer> getLegalCostumers() throws SQLException {
		return legalCostumerDAO.getLegalCostumers();
	}
	
	/**
	 * Method that updates a Legal Costumer
	 * @param legalCostumer Legal Costumer
	 * @return false if the legal Costumer don't exist
	 */
	public boolean update(LegalCostumer legalCostumer) {
		return update(legalCostumer);
	}
	
	/**
	 * Method that count the legal costumers list size
	 * @return Size of legal costumers list
	 * @throws SQLException 
	 */
	public int count() throws SQLException {
		return legalCostumerDAO.getLegalCostumers().size();
	}

}
